import java.util.ArrayList;
import java.util.Collections;

/* eg1:
有若干个学生，学生信息：姓名、年龄、分数（小数），已知每个学生对象都在ArrayList中存放，请输出每个学生的信息。
并且按照分数进行排序
 */
class Student implements Comparable<Student> { //要进行排序，就必须实现比较，要实现比较就必须实现Comparable的接口
    private String name;
    private int age;
    private double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override //重写compareTo方法
    public int compareTo(Student o) {
        return (int)(this.score - o.score);
    }
}
public class Test {

    public static void main(String[] args) {
        ArrayList<Student> arrayList = new ArrayList<>(); //arrayList是一个集合变量
        arrayList.add(new Student("小王",20,99.5));
        arrayList.add(new Student("小张",19,98.5));
        arrayList.add(new Student("小赵",21,95.5));

        //先看一下此时的学生类
        for (Student s : arrayList) {
            System.out.println(s);
        }
        System.out.println("排序后==================");
        //排序--必须用Collections集合类
        Collections.sort(arrayList); //要进行排序，就必须实现比较，要实现比较就必须实现Comparable的接口
        for (Student s : arrayList) {
            System.out.println(s);
        }
    }
}
